package admin;

import entity.*;
import helper.SimpleJSONObject;
import java.io.*;
import javax.ejb.*;
import javax.naming.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;

/**
 *
 * @author Sergey
 */
public class DeleteUser extends HttpServlet {

    public static final String answer = "answer";
    public static final String ok = "OK";
    public static final String wrong = "WRONG";
    public static final String idParameter = "id";
    public static final String splitter = ";";

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String idsString = request.getParameter(idParameter);
        String[] ids = idsString.split(splitter);
        Integer[] idsNumber = new Integer[ids.length];
        for (int i = 0; i < ids.length; i++) {
            idsNumber[i] = new Integer(ids[i]);
        }
        SimpleJSONObject json = new SimpleJSONObject();
        try {

            InitialContext context = new InitialContext();
            UsersLocalHome homeUser = (UsersLocalHome) context.lookup("java:comp/env/Users");
            for (int i = 0; i < idsNumber.length; i++) {
                homeUser.remove(idsNumber[i]);
            }
            json.addProperty(answer, ok);
        } catch (NamingException ex) {
            json.addProperty(answer, wrong);
        } catch (RemoveException ex) {
            json.addProperty(answer, wrong);
        } catch (EJBException ex) {
            json.addProperty(answer, wrong);
        }
        sendJSON(json, response);
    }

    private void sendJSON(SimpleJSONObject json, HttpServletResponse response) {
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter out = null;
        try {
            out = response.getWriter();
            out.println(json);
            out.flush();
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            out.close();
        }
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
